package com.yunsc.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.common.utils.PageResult;
import com.yunsc.store.dao.YunscRegionDao;
import com.yunsc.store.dao.YunscStoreAddressDao;
import com.yunsc.store.dto.QueryStoreAddressDTO;
import com.yunsc.store.dto.StoreAddressDTO;
import com.yunsc.store.entity.YunscStoreAddressEntity;
import com.yunsc.store.service.YunscStoreAddressService;
import com.yunsc.store.vo.AddressRegionVO;
import com.yunsc.store.vo.StoreAddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("yunscStoreAddressService")
public class YunscStoreAddressServiceImpl extends ServiceImpl<YunscStoreAddressDao, YunscStoreAddressEntity> implements YunscStoreAddressService {

    @Resource
    private YunscRegionDao regionDao;

    // 查询门店地址列表
    @Override
    public PageResult<StoreAddressVO> queryStoreAddressList(QueryStoreAddressDTO storeAddressDTO, Integer storeId) {
        String search = storeAddressDTO.getSearch();
        Integer type = storeAddressDTO.getType();
        Integer currentPage = storeAddressDTO.getPage();
        Integer pageSize = storeAddressDTO.getPageSize();
        Page<YunscStoreAddressEntity> page = new Page<>(currentPage, pageSize);
        Page<StoreAddressVO> pageResult = this.baseMapper.selectStoreAddress(page, search, type, storeId);
        List<StoreAddressVO> list = pageResult.getRecords();
        return PageResult.formatPageResult(list, currentPage, pageSize, pageResult.getTotal());
    }

    @Override
    @Transactional
    public void addStoreAddress(StoreAddressDTO addressDTO, Integer storeId) {
        StoreAddressVO addressVO = addressDTO.getForm();
        YunscStoreAddressEntity address = new YunscStoreAddressEntity();
        List<Integer> cascader = addressVO.getCascader();
        address.setProvinceId(cascader.get(0));
        address.setCityId(cascader.get(1));
        address.setRegionId(cascader.get(2));
        address.setDetail(addressVO.getDetail());
        address.setName(addressVO.getName());
        address.setPhone(addressVO.getPhone());
        address.setSort(addressVO.getSort());
        address.setType(addressVO.getType());
        address.setStoreId(storeId);
        this.save(address);
    }

    @Override
    @Transactional
    public void editStoreAddress(StoreAddressDTO addressDTO, Integer storeId) {
        StoreAddressVO addressVO = addressDTO.getForm();
        Integer addressId = addressDTO.getAddressId();
        YunscStoreAddressEntity addressEntity = this.getById(addressId);
        if (addressEntity == null) {
            throw new YunscException("地址不存在");
        }
        BeanUtils.copyProperties(addressVO, addressEntity);
        List<Integer> cascader = addressVO.getCascader();
        addressEntity.setProvinceId(cascader.get(0));
        addressEntity.setCityId(cascader.get(1));
        addressEntity.setRegionId(cascader.get(2));
        addressEntity.setAddressId(addressId);
        this.updateById(addressEntity);
    }

    @Override
    public void deleteStoreAddress(Integer addressId) {
        this.removeById(addressId);
    }

    @Override
    public List<StoreAddressVO> queryStoreAddressAll(Integer type, Integer storeId) {
        LambdaQueryWrapper<YunscStoreAddressEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscStoreAddressEntity::getIsDelete, 0);
        lqw.eq(YunscStoreAddressEntity::getStoreId, storeId);
        lqw.eq(YunscStoreAddressEntity::getType, type);
        List<YunscStoreAddressEntity> list = this.list(lqw);
        List<StoreAddressVO> collect = list.stream().map(item -> {
            StoreAddressVO addressVO = new StoreAddressVO();
            BeanUtils.copyProperties(item, addressVO);
            AddressRegionVO regionVO = regionDao.getAddressVOByOrderId(item.getRegionId());
            addressVO.setRegion(regionVO);
            List<Integer> cascader = new ArrayList<>();
            cascader.add(item.getProvinceId());
            cascader.add(item.getCityId());
            cascader.add(item.getRegionId());
            addressVO.setCascader(cascader);
            String fullAddress = regionVO.getProvince() + regionVO.getCity() + regionVO.getRegion() + item.getDetail();
            addressVO.setFullAddress(fullAddress);
            return addressVO;
        }).collect(Collectors.toList());
        return collect;

    }


}